module GiteeTransfer
  class Git
    LOCAL_REPO_DIR = 'repository'.freeze

    # Parameter Example
    # options:
    #   repo_url: 'http://106.12.126.145:3000/enterprise__ningboceshi/abc.git'
    #   namespace: 'enterprise'
    def clone(options)
      return true if File.directory?(local_repo_path(options))

      cmd = "git clone --bare #{options[:repo_url]} #{local_repo_path(options)}"
      GiteeTransfer.logger.info cmd
      result = system cmd
      GiteeTransfer.logger.error "[ERROR]: Clone failed. options: #{options}" unless result
      result
    end

    # Parameter Example
    # options:
    #   repo_url: 'http://106.12.126.145:3000/enterprise__ningboceshi/abc.git'
    #   namespace: 'enterprise'
    def push(options)
      Dir.chdir local_repo_path(options)
      cmd = "git push --mirror #{options[:repo_url]}"
      GiteeTransfer.logger.info cmd
      result = system cmd
      GiteeTransfer.logger.error "[ERROR]: Push failed. options: #{options}" unless result
      Dir.chdir GiteeTransfer.pwd
      result
    end

    private

    def local_repo_path(options)
      File.join(GiteeTransfer.pwd,  LOCAL_REPO_DIR, options[:namespace], File.basename(options[:repo_url]))
    end
  end
end
